package com.fw.service.persistence.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.persistence.SampleService;
import com.fw.service.persistence.dao.SampleDAO;
import com.fw.service.persistence.dto.Sample;

public class SampleServiceImpl implements SampleService {

    private static Logger logger = Logger.getLogger(SampleServiceImpl.class);

    @Autowired
    private SampleDAO sampleDAO;

    public Sample getSampleById(Long id) {
	try {
	    Sample sample = sampleDAO.findOne(id);
	    return sample;
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.error("Error " + e.getMessage());
	}
	return null;
    }

    public List<Sample> searchSampleByText(String text) {
	try {
	    List<Sample> samples = sampleDAO.findByText(text);
	    return samples;
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.error("Error " + e.getMessage());
	}
	return null;
    }

    public Sample saveSample(Sample sample) {
	try {
	    sampleDAO.save(sample);
	    return sample;
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    public Sample updateSample(Sample sample) {
	try {
	    sampleDAO.saveAndFlush(sample);
	    return sample;
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.error("Error " + e.getMessage());
	}
	return null;
    }

    public Boolean deleteSample(Sample sample) {
	try {
	    sampleDAO.delete(sample);
	    return true;
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.error("Error " + e.getMessage());
	}
	return false;
    }

    public Boolean deleteSampleById(Long id) {
	try {
	    sampleDAO.delete(id);
	    return true;
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.error("Error " + e.getMessage());
	}
	return false;
    }

}
